package net.pimpas.persistence.hibernate.visitor;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;

import net.pimpas.core.visitor.BaseClassVisitor;
import net.pimpas.persistence.entity.ann.EntityPackageAnnotation;
import net.pimpas.util.reflection.ReflectionUtil;

public class EntityVisitor extends BaseClassVisitor {
	
	public static final List<Class> entitys = new ArrayList<Class>();
	//private static final List<PackageVisited> packageVisiteds = new ArrayList<PackageVisited>();

	@Override
	public void visitClass(Class clazz) {
		/*
		if(this.havePackageAnnotation(clazz, clazz.getPackage()))
			this.getEntitys().add(clazz);
		*/	
		
		Package pack = clazz.getPackage();
		if(!clazz.isAnnotation() && !clazz.isEnum() && !clazz.isInterface())
			if(pack.getAnnotation(EntityPackageAnnotation.class) != null ||
					ReflectionUtil.checkAnnotationInClass(Entity.class, clazz))
				this.getEntitys().add(clazz);
	}
	/*
	private boolean havePackageAnnotation(Class clazz, Package pack) {
		if(clazz.isAnnotation() || clazz.isEnum() || clazz.isInterface())
			return false;
		
		if(pack == null) return false;
		
		// check in visiteds
		for(int i=0;i<packageVisiteds.size();i++)
			if(packageVisiteds.get(i).getPackageName().equals(pack.getName()))
				return packageVisiteds.get(i).getIsEntity();
		
		String packageName = pack.getName();
		
		if(pack.getAnnotation(EntityPackageAnnotation.class) != null) {
			packageVisiteds.add(new PackageVisited(packageName, true));
			return true;
		}
		
		packageVisiteds.add(new PackageVisited(packageName, false));
		
		String[] packages = packageName.split("\\.");
		boolean b = false;
		String pkg = "";
		
		for(int i=0;i<packages.length;i++) {
			if(pkg.length() > 0)
				pkg += "." + packages[i];
			else
				pkg = packages[i];
			
			b = this.havePackageAnnotation(clazz, Package.getPackage(pkg));
			if(b) return b;
		}
		
		return false;
	}
	*/
	public List<Class> getEntitys() {
		return entitys;
	}
	
	/*
	private static class PackageVisited {
		private String packageName;
		private Boolean isEntity;
		
		public PackageVisited(String packageName, boolean isEntity) {
			this.setPackageName(packageName);
			this.setIsEntity(isEntity);
		}
		
		public Boolean getIsEntity() {
			return isEntity;
		}
		public void setIsEntity(Boolean isEntity) {
			this.isEntity = isEntity;
		}
		public String getPackageName() {
			return packageName;
		}
		public void setPackageName(String packageName) {
			this.packageName = packageName;
		}
	}
	*/
}